#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Kernel PCA ã‚«ãƒ¼ãƒãƒ«ä¸»æˆåˆ†åˆ†æž
http://en.wikipedia.org/wiki/Kernel_principal_component_analysis
"""

import numpy as np
import matplotlib.pyplot as plt

# Polynomial kernel
k = lambda x, y: (np.dot(x,y) + 1.0)**2

A = np.array([[5,3,2,1],[15,10,1,1],[9,2,1,2],[9,5,3,2]])

N = len(A)
# kernel matrix
K = np.zeros((N,N))
for i in range(N):
    for j in range(i,N):
        K[i,j] = K[j,i] = k(A[i,:], A[j,:])

print K
# centered kernel matrix
ones = np.mat(np.ones((N,N))) / N
K = K - ones * K - K * ones + ones * K * ones

print "centered kernel matrix:"
print K